package com.tm.szb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Equals;
import com.tm.szb.commons.R;
import com.tm.szb.commons.ResultEnums;
import com.tm.szb.dto.EmployeeDTO;
import com.tm.szb.dto.LoginDto;
import com.tm.szb.entity.Employee;
import com.tm.szb.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;

@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("login")
    public R<Employee> login(@RequestBody LoginDto loginDto, HttpServletRequest request){
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        //接收到前端请求过来的参数
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(username)){
            return R.error(ResultEnums.LOGIN_ERROR_3);
        }


        Employee employee = employeeService.getByUserName(username);
        //如果对象为空 表示用户不存在
        if (employee == null){
            return R.error(ResultEnums.LOGIN_ERROR_1);
        }

        //校验员工状态是否为禁用状态
        if (employee.getStatus() == 0){
            return R.error(ResultEnums.LOGIN_ERROR_2);
        }

        //校验密码是否正确
        if (DigestUtils.md5DigestAsHex(password.getBytes()).equals(employee.getPassword())){
            employee.setPassword("");
            request.getSession().setAttribute("employee",employee.getId());
            return R.success(employee);
        }
        //密码校验失败
        return R.error(ResultEnums.LOGIN_ERROR_1);
    }


    @PostMapping("logout")
    public R logOut(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success();
    }


    @PostMapping
    public R addEmployee(@RequestBody EmployeeDTO employeeDTO,HttpServletRequest request){
        Long employeeId = (Long) request.getSession().getAttribute("employee");
        employeeService.addEmployee(employeeDTO,employeeId);
        return R.success();

    }


    @GetMapping("page")
    public R page(    @RequestParam(required = false,defaultValue = "1") Integer page,
                      @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                      @RequestParam(required = false) String name){

      Page<Employee> pageList = employeeService.findPageList(page,pageSize,name);
      return R.success(pageList);
    }



    @PutMapping
    public R updateStatus(@RequestBody EmployeeDTO employeeDTO,HttpServletRequest request){
        Long employeeId = (Long) request.getSession().getAttribute("employee");
        employeeService.updateStatus(employeeDTO,employeeId);
        return R.success();
    }


    @GetMapping("/{id}")
    public R updateEmployee(@PathVariable String id){

        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
